﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework; 

namespace GameEngine
{
    public class InputEngine
    {
        static MouseState _stateMouse;
        static KeyboardState _stateKeyboard;
        static bool _stateFocused = false;
        static bool[] _stateKeys = new bool[256];
        
        static public void Update(GameTime gameTime)
        {
            System.Windows.Forms.Form f = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(Engine.GetInstance.Window.Handle);
            if (f.Focused == false)
            {
                _stateFocused = false;
                return;
            }

            _stateKeyboard = Keyboard.GetState();
            _stateMouse = Mouse.GetState();

            Keys[] keystate = _stateKeyboard.GetPressedKeys();
            Array.Clear(_stateKeys, 0, _stateKeys.Length);
            foreach (Keys k in keystate)
            {
                int index = (int)k;
                _stateKeys[index] = true;
            }

            float xDifference = _stateMouse.X - 200;
            float yDifference = _stateMouse.Y - 200;

            if (_stateFocused == false)
            {
                xDifference = 0f;
                yDifference = 0f;
            }

            _stateFocused = true;

            Camera.Yaw = Camera.Yaw - xDifference * .002f;
            Camera.Pitch = Camera.Pitch - yDifference * .002f;
            Mouse.SetPosition(200, 200);
        }


        public static bool IsKeyDown(Keys key)
        {
            return _stateKeys[(int)key];
        }
        public static bool IsLeftMouseDown()
        {
            return _stateMouse.LeftButton == ButtonState.Pressed;
        }
        public static bool IsRightMouseDown()
        {
            return _stateMouse.RightButton == ButtonState.Pressed;
        }
 
    }
}
